package vip.liux.backend.application.service.coupon;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.math.NumberUtils;
import vip.liux.contracts.shared.enums.Status;
import vip.liux.core.model.NameValue;

import java.time.LocalDateTime;

@Data
@EqualsAndHashCode(callSuper = true)
public class ShopCouponDto extends AbsCouponDto {

    /**
     * 优惠券id
     */
    private Long id;

    /**
     * 优惠券编号
     */
    private String couponNo;

    /**
     * 领取开始时间
     */
    private LocalDateTime receiveStart;

    /**
     * 领取截止时间
     */
    private LocalDateTime receiveEnd;

    /**
     * 总数
     */
    private Long receiveNum;

    /**
     * 每人可领取张数
     */
    private Long receiveNumLimit;

    /**
     * 可领取数量
     */
    private Long quantityAvailable;

    /**
     * 优惠券状态
     */
    private Status status;

    /**
     * 是否已领取
     */
    public boolean received;

    private NameValue<Integer> state;

    public NameValue<Integer> getState() {
        if (received) {
            return new NameValue<>("已领取", 0);
        } else if (NumberUtils.compare(quantityAvailable, 0L) <= 0) {
            return new NameValue<>("已抢光", 0);
        } else  if (receiveEnd.isBefore(LocalDateTime.now())) {
            return new NameValue<>("已过期", 0);
        } else {
            return new NameValue<>("正常", 1);
        }
    }
}
